<template>
    <el-dialog
            :title="dialogParams.title"
            :visible.sync="dialogParams.visible"
            :close-on-click-modal="false"
            width="600px"
            append-to-body>
        <el-form :model="formData" :disabled="dialogParams.type === 'detail'" ref="formData" label-width="100px" class="demo-fileForm">
            <el-form-item label="角色：" prop="name">
                <el-select v-model="formData.roleIds " filterable multiple placeholder="请选择">
                    <el-option
                            v-for="item in roleArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close('formData')">取 消</el-button>
            <el-button type="primary" @click="submit('formData')">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import {getAllRole, getUserRoles, editUserRole} from "@/api/systemManage";

    export default {
        name: 'setUserRole',
        props: {
            dialogParams: {
                type: Object
            }
        },
        data () {
            return {
                roleArr: [],
                formData: {
                    userId: '',
                    roleIds : []
                }
            }
        },
        created () {
            this.getRole();
            this.getUserRoles();
        },
        methods: {
            // 获取角色下拉数据
            getRole () {
                getAllRole().then(res => {
                    this.roleArr = res || []
                }).catch()
            },
            // 根据id获取用户角色
            getUserRoles () {
                if (this.dialogParams.type != 'add' && this.dialogParams.id) {
                    let params = {
                        pageIndex: 1,
                        pageSize: 100,
                        userId: this.dialogParams.id
                    };
                    getUserRoles(params).then(res => {
                        let tempRole = res.dataList || [];
                        if (tempRole && tempRole.length) {
                            this.formData.roleIds = tempRole.map(item => item.roleId)
                        }
                    }).catch(err => {})
                }
            },
            // 保存数据
            submit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        switch (this.dialogParams.type) {
                            case 'role':
                                this.formData.userId = this.dialogParams.id;
                                editUserRole(this.formData).then(res => {
                                    this.$message.success('操作成功');
                                    this.$emit('getList');
                                    this.dialogParams.visible = false;
                                }).catch(()=>{});
                                break;
                        }
                    } else {
                        return false;
                    }
                });
            },
            // 关闭
            close (formName) {
                this.$refs[formName].resetFields();
                this.dialogParams.visible = false;
            }
        }
    }
</script>

<style scoped>

</style>